/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainWin.java
 *
 * Created on Mar 31, 2009, 5:36:22 PM
 */

package windows;

import etc.Database;
import etc.DocOperations;
import etc.FileSelectionClass;
import etc.NoFileSelectedException;
import etc.XMLOperations;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.DefaultTableModel;
import javax.xml.parsers.ParserConfigurationException;

/**
 *
 * @author Zoli
 */
public class MainWin extends javax.swing.JFrame {



    private String query;
    private Vector data1;
    private Vector data;
    private Vector wheredata;
    private String orderby;
    private String groupby;
    private Vector table4;
    private String primarykey;


    /** Creates new form MainWin */
    public MainWin(String s) {
       
       initComponents();
       
       if (s.compareTo("user")!=0)
       {
          this.jTabbedPane1.setVisible(false);
          this.jButton4.setEnabled(false);
          ExecuteMenuItem.setEnabled(false);
       }
       else
       {
            Database d=new Database();
            d=d.getInstance();
            d.connect();

            this.query=new String();
            this.orderby=new String("");
            this.groupby=new String("");
            this.primarykey=new String("");
            this.data=new Vector();
            this.wheredata=new Vector();
            this.data1=new Vector();
            this.table4=new Vector();

            this.refreshTableLists();
            
       }
      
        
    }

    private void refreshTableLists()
    {
        Database d=new Database();
            d=d.getInstance();
            d.connect();

        Vector tables=new Vector();
            tables=d.getTableNames();
            this.tableList.setListData(tables);
            this.tableList2.setListData(tables);
            this.tableList3.setListData(tables);
            this.tableList4.setListData(tables);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        queryText = new javax.swing.JTextArea();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tableList = new javax.swing.JList();
        jScrollPane3 = new javax.swing.JScrollPane();
        tablefieldList = new javax.swing.JList();
        jButton1 = new javax.swing.JButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        selectedList = new javax.swing.JList();
        jLabel4 = new javax.swing.JLabel();
        whereField = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jToggleButton1 = new javax.swing.JToggleButton();
        jButton3 = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        ascRadioButton = new javax.swing.JRadioButton();
        descRadioButton = new javax.swing.JRadioButton();
        jToggleButton2 = new javax.swing.JToggleButton();
        eRButton = new javax.swing.JRadioButton();
        gRButton = new javax.swing.JRadioButton();
        sRButton = new javax.swing.JRadioButton();
        seRButton = new javax.swing.JRadioButton();
        geRButton = new javax.swing.JRadioButton();
        neRButton = new javax.swing.JRadioButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane5 = new javax.swing.JScrollPane();
        tableList2 = new javax.swing.JList();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jButton5 = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane6 = new javax.swing.JScrollPane();
        deleteTable = new javax.swing.JTable();
        jScrollPane7 = new javax.swing.JScrollPane();
        tableList3 = new javax.swing.JList();
        jButton6 = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane8 = new javax.swing.JScrollPane();
        tableList4 = new javax.swing.JList();
        jButton7 = new javax.swing.JButton();
        jScrollPane9 = new javax.swing.JScrollPane();
        updateTable = new javax.swing.JTable();
        jPanel5 = new javax.swing.JPanel();
        tableNameField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jScrollPane10 = new javax.swing.JScrollPane();
        columnTable = new javax.swing.JTable();
        jButton8 = new javax.swing.JButton();
        jButton9 = new javax.swing.JButton();
        jButton10 = new javax.swing.JButton();
        jButton11 = new javax.swing.JButton();
        jLabel11 = new javax.swing.JLabel();
        engineComboBox = new javax.swing.JComboBox();
        jButton12 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JSeparator();
        ExecuteMenuItem = new javax.swing.JMenuItem();
        jSeparator3 = new javax.swing.JSeparator();
        jMenuItem5 = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        jMenuItem7 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem8 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Query Manager");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        queryText.setColumns(20);
        queryText.setRows(5);
        jScrollPane1.setViewportView(queryText);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel2.setText("Tables");

        jLabel3.setText("Table Fields");

        tableList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tableList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                tableListValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(tableList);

        jScrollPane3.setViewportView(tablefieldList);

        jButton1.setText("->");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        selectedList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane4.setViewportView(selectedList);

        jLabel4.setText("Selected Fields");

        jLabel5.setText("Where selected field is:");

        jButton2.setText("Add");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jToggleButton1.setText("Order by selected field");
        jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton1ActionPerformed(evt);
            }
        });

        jButton3.setText("<-");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel6.setText("Note: String should be placed between \" \"");

        buttonGroup1.add(ascRadioButton);
        ascRadioButton.setText("Ascending");

        buttonGroup1.add(descRadioButton);
        descRadioButton.setText("Descending");
        descRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                descRadioButtonActionPerformed(evt);
            }
        });

        jToggleButton2.setText("Group by selected field");
        jToggleButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton2ActionPerformed(evt);
            }
        });

        buttonGroup2.add(eRButton);
        eRButton.setText("=");

        buttonGroup2.add(gRButton);
        gRButton.setText(">");
        gRButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                gRButtonActionPerformed(evt);
            }
        });

        buttonGroup2.add(sRButton);
        sRButton.setText("<");

        buttonGroup2.add(seRButton);
        seRButton.setText("<=");

        buttonGroup2.add(geRButton);
        geRButton.setText(">=");

        buttonGroup2.add(neRButton);
        neRButton.setText("!=");
        neRButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                neRButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(122, 122, 122)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(37, 37, 37)
                        .addComponent(jLabel2)
                        .addGap(109, 109, 109)
                        .addComponent(jLabel3)
                        .addGap(189, 189, 189)
                        .addComponent(jLabel4))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(28, 28, 28)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 56, Short.MAX_VALUE))
                        .addGap(46, 46, 46)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(30, 30, 30)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jToggleButton2, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ascRadioButton, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(descRadioButton, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jToggleButton1, javax.swing.GroupLayout.Alignment.LEADING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 262, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(eRButton)
                    .addComponent(gRButton)
                    .addComponent(sRButton)
                    .addComponent(seRButton)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(geRButton)
                            .addComponent(neRButton))
                        .addGap(34, 34, 34)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(whereField, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton2)))))
                .addGap(77, 77, 77))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(15, 15, 15)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel4))
                                .addGap(38, 38, 38)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)
                                        .addComponent(jScrollPane3))
                                    .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(59, 59, 59)
                                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(30, 30, 30)
                                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(46, 46, 46)
                        .addComponent(jLabel5)
                        .addGap(18, 18, 18)
                        .addComponent(jToggleButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ascRadioButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(descRadioButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jToggleButton2))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(eRButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(gRButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(sRButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(geRButton)
                            .addComponent(whereField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton2))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(seRButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(neRButton))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(44, 44, 44)
                                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(70, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("SELECT", jPanel1);

        tableList2.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane5.setViewportView(tableList2);

        jLabel7.setText("Tables");

        jLabel8.setText("Select table into which to insert a new row");

        jButton5.setText("OK");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addComponent(jLabel8)
                        .addGap(53, 53, 53)
                        .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(315, 315, 315)
                        .addComponent(jLabel7))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(315, 315, 315)
                        .addComponent(jButton5)))
                .addContainerGap(1064, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(jLabel7)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(83, 83, 83)
                        .addComponent(jLabel8)))
                .addGap(27, 27, 27)
                .addComponent(jButton5)
                .addContainerGap(38, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("INSERT", jPanel2);

        deleteTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane6.setViewportView(deleteTable);

        tableList3.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                tableList3ValueChanged(evt);
            }
        });
        jScrollPane7.setViewportView(tableList3);

        jButton6.setText("Delete");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel9.setText("Note: You can delete a row only if it's not referenced by a foreign key.");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(88, 88, 88)
                        .addComponent(jButton6))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addComponent(jScrollPane7, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane6, javax.swing.GroupLayout.PREFERRED_SIZE, 998, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(237, 237, 237)
                        .addComponent(jLabel9)))
                .addContainerGap(289, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jScrollPane7, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton6))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jScrollPane6, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel9)))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("DELETE", jPanel3);

        tableList4.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                tableList4ValueChanged(evt);
            }
        });
        jScrollPane8.setViewportView(tableList4);

        jButton7.setText("Update");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        updateTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane9.setViewportView(updateTable);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addComponent(jScrollPane8, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGap(90, 90, 90)
                        .addComponent(jButton7)))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane9, javax.swing.GroupLayout.PREFERRED_SIZE, 998, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(286, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(jScrollPane8, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton7))
                    .addComponent(jScrollPane9, javax.swing.GroupLayout.DEFAULT_SIZE, 261, Short.MAX_VALUE))
                .addContainerGap())
        );

        jTabbedPane1.addTab("UPDATE", jPanel4);

        tableNameField.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                tableNameFieldCaretUpdate(evt);
            }
        });
        tableNameField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tableNameFieldActionPerformed(evt);
            }
        });

        jLabel1.setText("Table name:");

        jLabel10.setText("Columns:");

        columnTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Cloumn Name", "Data Type", "Not NULL", "Auto INC", "Default Value"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        columnTable.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                columnTablePropertyChange(evt);
            }
        });
        columnTable.addVetoableChangeListener(new java.beans.VetoableChangeListener() {
            public void vetoableChange(java.beans.PropertyChangeEvent evt)throws java.beans.PropertyVetoException {
                columnTableVetoableChange(evt);
            }
        });
        jScrollPane10.setViewportView(columnTable);

        jButton8.setText("Add Column");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });

        jButton9.setText("Remove Column");
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });

        jButton10.setText("Edit Column");
        jButton10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton10ActionPerformed(evt);
            }
        });

        jButton11.setText("Foreign Keys");
        jButton11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton11ActionPerformed(evt);
            }
        });

        jLabel11.setText("Engine:");

        engineComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "InnoDB", "MyISAM", "MEMORY", "BerkeleyDB", "BLACKHOLE", "EXAMPLE", "ARCHIVE", "CVS", "ndbcluster", "FEDERATED", "MRG_MYISAM", "ISAM" }));

        jButton12.setText("Add Primary Key");
        jButton12.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton12ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGap(51, 51, 51)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(tableNameField, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(69, 69, 69)
                        .addComponent(jLabel11)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(engineComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(jScrollPane10, javax.swing.GroupLayout.PREFERRED_SIZE, 675, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(126, 126, 126)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jButton11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton12, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap(450, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tableNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11)
                    .addComponent(engineComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane10, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)
                            .addGroup(jPanel5Layout.createSequentialGroup()
                                .addComponent(jButton8)
                                .addGap(18, 18, 18)
                                .addComponent(jButton9)
                                .addGap(18, 18, 18)
                                .addComponent(jButton10)
                                .addGap(18, 18, 18)
                                .addComponent(jButton12)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 16, Short.MAX_VALUE)
                                .addComponent(jButton11)))
                        .addContainerGap())
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGap(36, 36, 36)
                        .addComponent(jLabel10))))
        );

        jTabbedPane1.addTab("CREATE TABLE", jPanel5);

        jButton4.setText("Execute");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jMenu1.setText("File");
        jMenu1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenu1ActionPerformed(evt);
            }
        });

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem1.setText("New Query");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem2.setText("Open Query...");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);

        jMenuItem3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem3.setText("Save Query...");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem3);
        jMenu1.add(jSeparator2);

        ExecuteMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));
        ExecuteMenuItem.setText("Execute Query");
        ExecuteMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ExecuteMenuItemActionPerformed(evt);
            }
        });
        jMenu1.add(ExecuteMenuItem);
        jMenu1.add(jSeparator3);

        jMenuItem5.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_I, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem5.setText("Import Query...");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem5);
        jMenu1.add(jSeparator1);

        jMenuItem7.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem7.setText("Exit");
        jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem7ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem7);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Help");

        jMenuItem8.setText("About");
        jMenuItem8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem8ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem8);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(584, 584, 584)
                        .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(36, 36, 36)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 547, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 1531, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(76, 76, 76)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 175, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(87, 87, 87))))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem7ActionPerformed
       
        this.dispose();
    }//GEN-LAST:event_jMenuItem7ActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
       
    }//GEN-LAST:event_formWindowClosing


    private void refreshquery()
    {
        if (this.data.size()>0)
        {
        this.query="SELECT ";

        for (int i=0;i<data.size();i++)
        {
            this.query=this.query + data.get(i) + ",";
        }

        this.query=this.query.substring(0, this.query.length()-1);
        this.query=this.query + " FROM ";

        for (int i=0;i<this.data1.size();i++)
        {
            this.query=this.query+this.data1.get(i)+",";
        }

        this.query=this.query.substring(0, this.query.length()-1);

        if (this.wheredata.size()>0)
        {
            this.query=this.query+" WHERE ";

            for (int i=0;i<this.wheredata.size();i++)
            {
                this.query = this.query + this.wheredata.get(i) + " ";
            }

            this.query=this.query.substring(0, this.query.length()-1);

        }

        if (this.orderby.length()>0)
        {
            this.query=this.query+this.orderby;
        }

        if (this.groupby.length()>0)
        {
            this.query=this.query+this.groupby;
        }

        this.query=this.query+";";
    }
        else
        {
            this.query="";
        }

        this.queryText.setText(this.query);
    }

    private int analyzeQueryType()
    {
        String s=new String();
        s=this.query.substring(0, this.query.indexOf(" "));
        
        if (s.compareTo("SELECT")==0)
        {
            return 0;
        }
        else if (s.compareTo("INSERT")==0)
        {
            return 1;
        }
        else if (s.compareTo("DELETE")==0)
        {
            return 1;
        }
        else if (s.compareTo("UPDATE")==0)
        {
            return 1;
        }
        else if (s.compareTo("CREATE")==0)
        {
            return 2;
        }
        else
        {
            return -1;
        }
    }

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:

        Database d=new Database();
        d=d.getInstance();
        d.connect();
        

        switch(this.analyzeQueryType())
        {
            case -1:
                JOptionPane.showMessageDialog(this, "Could not execute query. Check for syntax.","Error",JOptionPane.ERROR_MESSAGE);
                break;

            case 0:
                d.query(this.query);
                ResultWin reswin=new ResultWin(d.getQueryResult());
                reswin.setVisible(true);
                break;
            case 1:
                if (d.update(this.query))
                {
                    JOptionPane.showMessageDialog(this, "1 row was affected succesfully","INSERT",JOptionPane.INFORMATION_MESSAGE);
                }
                else
                {
                    JOptionPane.showMessageDialog(this, "An error occured","Error",JOptionPane.ERROR_MESSAGE);
                }
                break;
            case 2:
                if (d.update(this.query))
                {
                    JOptionPane.showMessageDialog(this, "1 row was affected succesfully","INSERT",JOptionPane.INFORMATION_MESSAGE);
                }
                else
                {
                    JOptionPane.showMessageDialog(this, "An error occured","Error",JOptionPane.ERROR_MESSAGE);
                }

        }
        this.refreshTableLists();
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jToggleButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton2ActionPerformed
        // TODO add your handling code here:
        if (this.jToggleButton2.isSelected()) {
            this.groupby=" GROUP BY " + this.selectedList.getModel().getElementAt(this.selectedList.getSelectedIndex());
        } else {
            this.groupby="";
        }

        this.refreshquery();
    }//GEN-LAST:event_jToggleButton2ActionPerformed

    private void descRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_descRadioButtonActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_descRadioButtonActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        int selected=this.selectedList.getSelectedIndex();
        String s=this.selectedList.getModel().getElementAt(selected).toString();

        for (int i=0;i<data.size();i++) {
            if (data.get(i).toString().compareTo(s)==0) {
                data.remove(i);
            }
        }



       for (int i=0;i<this.wheredata.size();i++)
       {
            if (this.wheredata.get(i).toString().contains(s))
            {
                this.wheredata.remove(i);
                if (i>0)
                {
                    if ((this.wheredata.get(i-1).toString().compareTo("AND")==0) || (this.wheredata.get(i-1).toString().compareTo("OR")==0))
                    {
                        this.wheredata.remove(i-1);
                    }
                }
            }
       }

        if (this.orderby.contains(s))
            this.orderby="";

        if (this.groupby.contains(s))
            this.groupby="";

        s=s.substring(0,s.indexOf('.'));
        boolean isanymore=false;

        for (int i=0;i<data.size();i++) {
            if (data.get(i).toString().substring(0, data.get(i).toString().indexOf('.')).compareTo(s)==0) {
                isanymore=true;
            }
        }

        if (!isanymore) {
            for (int i=0;i<data1.size();i++) {
                if (data1.get(i).toString().compareTo(s)==0) {
                    data1.remove(i);
                }
            }
        }



        this.selectedList.setListData(this.data);
        this.refreshquery();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton1ActionPerformed
        // TODO add your handling code here:

        if (this.jToggleButton1.isSelected()) {
            this.orderby=" ORDER BY " + this.selectedList.getModel().getElementAt(this.selectedList.getSelectedIndex());

            if (this.ascRadioButton.isSelected()) {
                this.orderby=this.orderby + " ASC";
            } else if (this.descRadioButton.isSelected()) {
                this.orderby=this.orderby + " DESC";
            } else {
                this.orderby=this.orderby + " ASC";
            }
        } else {
            this.orderby="";
        }

        this.refreshquery();
    }//GEN-LAST:event_jToggleButton1ActionPerformed


    private String getOperatorSelection()
    {
        if (this.eRButton.isSelected())
            return "=";
        else if (this.gRButton.isSelected())
            return ">";
        else if (this.geRButton.isSelected())
            return ">=";
        else if (this.sRButton.isSelected())
            return "<";
        else if (this.seRButton.isSelected())
            return "<=";
        else if (this.neRButton.isSelected())
            return "!=";
        else return "";
    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
if (this.selectedList.getSelectedIndex()==-1)
{
    JOptionPane.showMessageDialog(this, "No field was selected","Warning",JOptionPane.WARNING_MESSAGE);
}
else
{
        if (this.wheredata.size()>0)
        {
            ConditionDialog cond=new ConditionDialog(this,true);
            cond.setVisible(true);
            if (cond.getOperator().compareTo("")==0)
            {
                JOptionPane.showMessageDialog(this, "Invalid selection.","Error",JOptionPane.ERROR_MESSAGE);
            }
            else
            {
                if (this.getOperatorSelection().compareTo("")==0)
                {
                    JOptionPane.showMessageDialog(this, "No operator was selected","Warning",JOptionPane.WARNING_MESSAGE);
                }
                else
                {
                    this.wheredata.add(cond.getOperator());
                    this.wheredata.add(this.selectedList.getModel().getElementAt(this.selectedList.getSelectedIndex()).toString() + this.getOperatorSelection() + this.whereField.getText());
                    this.whereField.setText("");
                    this.refreshquery();
                }
            }

        }
        else
        {
            if (this.getOperatorSelection().compareTo("")==0)
            {
                JOptionPane.showMessageDialog(this, "No operator was selected","Warning",JOptionPane.WARNING_MESSAGE);
            }
            else
            {
                this.wheredata.add(this.selectedList.getModel().getElementAt(this.selectedList.getSelectedIndex()).toString() + this.getOperatorSelection() + this.whereField.getText());
                this.whereField.setText("");
                this.refreshquery();
            }
        }
}
}//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        int tables=tableList.getSelectedIndex();

        int fields[]=tablefieldList.getSelectedIndices();

        if ((tables==-1) || (fields.length==0)) {
            JOptionPane.showMessageDialog(this, "There is nothing to add to the query","Cannot generate query",JOptionPane.WARNING_MESSAGE);
        } else {

            for (int i=0;i<fields.length;i++) {
                if (!this.data.contains(tablefieldList.getModel().getElementAt(fields[i]).toString())) {
                    data.add(tableList.getModel().getElementAt(tables).toString() + "." + tablefieldList.getModel().getElementAt(fields[i]).toString());
                }

            }

            this.selectedList.setListData(data);


            if (!this.data1.contains(tableList.getModel().getElementAt(tables).toString())) {
                this.data1.add(tableList.getModel().getElementAt(tables).toString());
            }


            this.refreshquery();


        }
}//GEN-LAST:event_jButton1ActionPerformed

    private void tableListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_tableListValueChanged
        // TODO add your handling code here:
        Database d=new Database();
        d=d.getInstance();
        d.connect();
        d.query("select * from " + this.tableList.getModel().getElementAt(this.tableList.getSelectedIndex()).toString() + ";");
        this.tablefieldList.setListData(d.getColumnNames());
    }//GEN-LAST:event_tableListValueChanged

    private void gRButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_gRButtonActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_gRButtonActionPerformed

    private void neRButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_neRButtonActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_neRButtonActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // TODO add your handling code here:
        if (this.tableList2.getSelectedIndex()==-1)
        {
            JOptionPane.showMessageDialog(this, "No table was selected","Error",JOptionPane.ERROR_MESSAGE);
        }
        else
        {

        Database d=new Database();
        d=d.getInstance();
        d.connect();

        String sel=this.tableList2.getModel().getElementAt(this.tableList2.getSelectedIndex()).toString();
               
        d.query("SELECT * FROM " + sel + ";");

        Vector fields=new Vector();
        fields=d.getColumnNames();
        Vector values=new Vector();

        for (int i=0;i<fields.size();i++)
        {
            String s=new String("");
            while (s.compareTo("")==0)
            {
                s=JOptionPane.showInputDialog(this, "Input value for "+ fields.get(i), "INSERT INTO table", JOptionPane.OK_CANCEL_OPTION);
                if (s==null)
                    s="";
            }

            try
            {
                Integer.parseInt(s);
                values.add(s);
            }
            catch (NumberFormatException ex)
            {
                values.add("\"" + s + "\"");
            }
        }

        this.query="INSERT INTO " + sel + " (";

        for (int i=0;i<fields.size();i++)
        {
            this.query = this.query + fields.get(i) + ",";
        }

        this.query = this.query.substring(0, this.query.length()-1);
        this.query = this.query + ") VALUES (";

        for (int i=0;i<values.size();i++)
        {
            this.query = this.query + values.get(i) + ",";
        }

        this.query = this.query.substring(0, this.query.length()-1);
        this.query = this.query + ");";
        this.queryText.setText(this.query);
        }

    }//GEN-LAST:event_jButton5ActionPerformed

    private void ExecuteMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ExecuteMenuItemActionPerformed
        // TODO add your handling code here:
        Database d=new Database();
        d=d.getInstance();
        d.connect();


        switch(this.analyzeQueryType())
        {
            case -1:
                JOptionPane.showMessageDialog(this, "Could not execute query. Check for syntax.","Error",JOptionPane.ERROR_MESSAGE);
                break;

            case 0:
                d.query(this.query);
                ResultWin reswin=new ResultWin(d.getQueryResult());
                reswin.setVisible(true);
                break;
            case 1:
                if (d.update(this.query))
                {
                    JOptionPane.showMessageDialog(this, "1 row was affected succesfully","INSERT",JOptionPane.INFORMATION_MESSAGE);
                }
                else
                {
                    JOptionPane.showMessageDialog(this, "An error occured","Error",JOptionPane.ERROR_MESSAGE);
                }

        }
    }//GEN-LAST:event_ExecuteMenuItemActionPerformed

    private void tableList3ValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_tableList3ValueChanged
        // TODO add your handling code here:
        Database d=new Database();
        d=d.getInstance();
        d.connect();
        d.query("SELECT * FROM " + this.tableList3.getModel().getElementAt(this.tableList3.getSelectedIndex()).toString() + ";");
        this.deleteTable.setModel(d.getQueryResult());
    }//GEN-LAST:event_tableList3ValueChanged

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        // TODO add your handling code here:
        if (this.deleteTable.getSelectedRow()==-1)
        {
            JOptionPane.showMessageDialog(this, "There is nothing to delete.","Delete Row",JOptionPane.WARNING_MESSAGE);
        }
        else
        {
            int row=this.deleteTable.getSelectedRow();
            Vector values=new Vector();

            for (int i=0;i<this.deleteTable.getColumnCount();i++)
            {
                values.add(this.deleteTable.getModel().getValueAt(row, i));
            }

            Database d=new Database();
            d=d.getInstance();
            d.connect();
            d.query("SELECT * FROM " + this.tableList3.getModel().getElementAt(this.tableList3.getSelectedIndex()).toString() + ";");

            Vector fields=new Vector ();
            fields=d.getColumnNames();

            this.query="DELETE FROM " + this.tableList3.getModel().getElementAt(this.tableList3.getSelectedIndex()).toString() + " WHERE ";

            for (int i=0;i<fields.size();i++)
            {
                try
                {
                    Integer.parseInt(values.get(i).toString());
                    this.query = this.query + fields.get(i) + "=" + values.get(i) + " AND ";
                }
                catch (NumberFormatException ex)
                {
                    this.query = this.query + fields.get(i) + "=\"" + values.get(i) + "\" AND ";
                }
                
            }

            this.query = this.query.substring(0,this.query.length()-5);
            this.query=this.query+";";
            
            this.queryText.setText(this.query);
        }

    }//GEN-LAST:event_jButton6ActionPerformed

    private void tableList4ValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_tableList4ValueChanged
        // TODO add your handling code here:
         Database d=new Database();
        d=d.getInstance();
        d.connect();
        d.query("SELECT * FROM " + this.tableList4.getModel().getElementAt(this.tableList4.getSelectedIndex()).toString() + ";");
        this.updateTable.setModel(d.getQueryResult());

        this.table4.removeAllElements();

        for (int i=0;i<this.updateTable.getRowCount();i++)
        {
            Vector row=new Vector();
            for (int j=0;j<this.updateTable.getColumnCount();j++)
            {
                row.add(this.updateTable.getModel().getValueAt(i, j));
            }
            this.table4.add(row);
        }

    }//GEN-LAST:event_tableList4ValueChanged

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        // TODO add your handling code here:

        if (this.updateTable.getSelectedRow()==-1)
        {
            JOptionPane.showMessageDialog(this, "No row was selected for update.","Warning",JOptionPane.WARNING_MESSAGE);
        }
        else
        {

        Database d=new Database();
        d=d.getInstance();
        d.connect();
        d.query("SELECT * FROM " + this.tableList4.getModel().getElementAt(this.tableList4.getSelectedIndex()).toString() + ";");

        Vector fields=new Vector();
        
        fields=d.getColumnNames();

        String part2=new String();
        part2=" WHERE ";

        for (int i=0;i<this.updateTable.getColumnCount();i++)
            {
                part2 = part2 + fields.get(i) + "=";
                Vector a=new Vector();
                a=(Vector) this.table4.get(this.updateTable.getSelectedRow());

                try
                {
                    Integer.parseInt(a.get(i).toString());
                    part2 = part2 + a.get(i).toString() + " AND ";
                }
                catch (NumberFormatException ex)
                {
                    part2 = part2 + "\"" + a.get(i).toString() + "\" AND ";
                }
            }
        part2=part2.substring(0, part2.length()-4);

        this.query="UPDATE " + this.tableList4.getModel().getElementAt(this.tableList4.getSelectedIndex()).toString() + " SET ";

            for (int i=0;i<this.updateTable.getColumnCount();i++)
            {
                this.query = this.query + fields.get(i) + "=";
                try
                {
                    Integer.parseInt(this.updateTable.getModel().getValueAt(this.updateTable.getSelectedRow(), i).toString());
                    this.query = this.query + this.updateTable.getModel().getValueAt(this.updateTable.getSelectedRow(), i).toString() + ",";
                }
                catch (NumberFormatException ex)
                {
                    this.query = this.query + "\"" + this.updateTable.getModel().getValueAt(this.updateTable.getSelectedRow(), i).toString() + "\",";
                }
            }

        this.query = this.query.substring(0, this.query.length()-1);
        this.query = this.query + part2;
        this.query = this.query + ";";
        this.queryText.setText(this.query);
        }

    }//GEN-LAST:event_jButton7ActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        // TODO add your handling code here:
        if (this.queryText.getText().compareTo("")==0)
        {
            this.queryText.setText("");
            this.selectedList.setListData(new Vector());
            this.data1=new Vector();
            this.wheredata=new Vector();
            this.groupby="";
            this.orderby="";
            this.data=new Vector();
            
        }
        else
        {
            if (JOptionPane.showConfirmDialog(this, "Do you wish to save current query?","New Query",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE)==JOptionPane.YES_OPTION)
            {
                FileSelectionClass fil=new FileSelectionClass();
                fil.setFileFilter(new FileNameExtensionFilter("SQL Scripts(*.sql)","sql"));
                fil.save();
                if (!fil.wascancelled())
                {
                    try
                    {
                        DocOperations doc = new DocOperations(fil.getSelectedDocument().getPath() + ".sql");
                        doc.writeDocument(this.queryText.getText());
                    }
                    catch (NoFileSelectedException ex)
                    {
                        JOptionPane.showMessageDialog(this, ex.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
                    }

                }
            }

            this.queryText.setText("");
            this.selectedList.setListData(new Vector());
            this.data1=new Vector();
            this.wheredata=new Vector();
            this.groupby="";
            this.orderby="";
            this.data=new Vector();
        }
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        // TODO add your handling code here:
        
                FileSelectionClass fil=new FileSelectionClass();
                fil.setFileFilter(new FileNameExtensionFilter("SQL Scripts(*.sql)","sql"));
                fil.save();
                if (!fil.wascancelled())
                {
                    try
                    {
                        DocOperations doc = new DocOperations(fil.getSelectedDocument().getPath() + ".sql");
                        doc.writeDocument(this.queryText.getText());
                    }
                    catch (NoFileSelectedException ex)
                    {
                        JOptionPane.showMessageDialog(this, ex.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
                    }

                }
            
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void jMenu1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenu1ActionPerformed
        // TODO add your handling code here:
        
    }//GEN-LAST:event_jMenu1ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        // TODO add your handling code here:
         if (this.queryText.getText().compareTo("")==0)
        {
           FileSelectionClass fil=new FileSelectionClass();
           fil.setFileFilter(new FileNameExtensionFilter("SQL Scripts(*.sql)","sql"));
           fil.open();
           if (!fil.wascancelled())
           {
                try
                {
                    DocOperations op = new DocOperations(fil.getSelectedDocument().getPath());
                    this.query=op.readDocument();
                    this.queryText.setText(this.query);
                }
                catch (NoFileSelectedException ex)
                {
                    JOptionPane.showMessageDialog(this, ex.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
                }


           }

        }
        else
        {
            if (JOptionPane.showConfirmDialog(this, "Do you wish to save current query?","New Query",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE)==JOptionPane.YES_OPTION)
            {
                FileSelectionClass fil=new FileSelectionClass();
                fil.save();
                if (!fil.wascancelled())
                {
                    try
                    {
                        DocOperations doc = new DocOperations(fil.getSelectedDocument().getPath());
                        doc.writeDocument(this.queryText.getText());
                    }
                    catch (NoFileSelectedException ex)
                    {
                        JOptionPane.showMessageDialog(this, ex.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
                    }

                }
            }


        }
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jMenuItem8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem8ActionPerformed
        // TODO add your handling code here:
        AboutDialog a=new AboutDialog(this,true);
        a.setVisible(true);
    }//GEN-LAST:event_jMenuItem8ActionPerformed

    private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem5ActionPerformed
        // TODO add your handling code here:
        FileSelectionClass fil=new FileSelectionClass();
        fil.setFileFilter(new FileNameExtensionFilter("XML Files(*.xml)","xml"));
        fil.open();

        if (!fil.wascancelled())
        {
            XMLOperations xml=new XMLOperations();

            try
            {
                xml.setDocument(fil.getSelectedDocument().getPath());
                
                try
                {
                    xml.ParseDocument();
                }
                catch (ParserConfigurationException ex)
                {
                    JOptionPane.showMessageDialog(this, ex.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
                }
                ResultWin res=new ResultWin(xml.getTableData());
                res.setVisible(true);
            }
            catch (NoFileSelectedException ex)
            {
                JOptionPane.showMessageDialog(this, ex.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
            }


        }
    }//GEN-LAST:event_jMenuItem5ActionPerformed



    private void tableNameFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tableNameFieldActionPerformed
        // TODO add your handling code here:
        this.query="CREATE TABLE " + this.tableNameField.getText() + " (";

        DefaultTableModel model=new DefaultTableModel();
        model=(DefaultTableModel) this.columnTable.getModel();

        for (int i=0;i<model.getRowCount();i++)
        {
            

            this.query=this.query + "'" + model.getValueAt(i, 0) + "' " + model.getValueAt(i,1);

            if (model.getValueAt(i, 2).toString().compareTo("true")==0)
            {
                this.query=this.query + " NOT NULL";
            }

            if (model.getValueAt(i, 4).toString().compareTo("")!=0)
            {
                this.query=this.query + " DEFAULT " + model.getValueAt(i, 4).toString();
            }

            if (model.getValueAt(i, 3).toString().compareTo("true")==0)
            {
                this.query=this.query + " AUTO_INCREMENT";
            }

            this.query = this.query + ",";
            
        }

        if (this.query.charAt(this.query.length()-1)==',')
                this.query = this.query.substring(0,this.query.length()-1);
        this.query = this.query + ") ENGINE = " + this.engineComboBox.getModel().getSelectedItem().toString() + ";";
        this.queryText.setText(this.query);
}//GEN-LAST:event_tableNameFieldActionPerformed

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
        // TODO add your handling code here:
        AddColumnDialog dialog=new AddColumnDialog(this,true);
        dialog.setTitle("Add New Column");
        dialog.setVisible(true);
        if (dialog.getValues().size()>0)
        {
            Vector columnnames=new Vector();
            columnnames.add("Column Name");
            columnnames.add("Data Type");
            columnnames.add("Not NULL");
            columnnames.add("Auto INC");
            columnnames.add("Default Value");

            Vector tabledata=new Vector();
            DefaultTableModel model=new DefaultTableModel();
            model=(DefaultTableModel) this.columnTable.getModel();

            for (int i=0;i<model.getRowCount();i++)
            {
                Vector v1=new Vector();
                for (int j=0;j<model.getColumnCount();j++)
                {
                    v1.add(model.getValueAt(i, j).toString());
                }
                tabledata.add(v1);
            }

            tabledata.add(dialog.getValues());
            model=new DefaultTableModel(tabledata,columnnames);
            this.columnTable.setModel(model);
           
        }
    }//GEN-LAST:event_jButton8ActionPerformed

    private void tableNameFieldCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_tableNameFieldCaretUpdate
        // TODO add your handling code here:
        this.query="CREATE TABLE " + this.tableNameField.getText() + " (";

        DefaultTableModel model=new DefaultTableModel();
        model=(DefaultTableModel) this.columnTable.getModel();

        for (int i=0;i<model.getRowCount();i++)
        {


            this.query=this.query + "`" + model.getValueAt(i, 0) + "` " + model.getValueAt(i,1);

            if (model.getValueAt(i, 2).toString().compareTo("true")==0)
            {
                this.query=this.query + " NOT NULL";
            }

            if (model.getValueAt(i, 4).toString().compareTo("")!=0)
            {
                this.query=this.query + " DEFAULT " + model.getValueAt(i, 4).toString();
            }

            if (model.getValueAt(i, 3).toString().compareTo("true")==0)
            {
                this.query=this.query + " AUTO_INCREMENT";
            }

            this.query = this.query + ",";

        }

        this.query = this.query + this.primarykey;

        if (this.query.charAt(this.query.length()-1)==',')
                this.query = this.query.substring(0,this.query.length()-1);
        this.query = this.query + ") ENGINE = " + this.engineComboBox.getModel().getSelectedItem().toString() + ";";
        this.queryText.setText(this.query);
    }//GEN-LAST:event_tableNameFieldCaretUpdate

    private void columnTableVetoableChange(java.beans.PropertyChangeEvent evt)throws java.beans.PropertyVetoException {//GEN-FIRST:event_columnTableVetoableChange
        // TODO add your handling code here:

    }//GEN-LAST:event_columnTableVetoableChange

    private void columnTablePropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_columnTablePropertyChange
        // TODO add your handling code here:
        if (jTabbedPane1.getSelectedIndex()==4)
        {
        this.query="CREATE TABLE " + this.tableNameField.getText() + " (";

        DefaultTableModel model=new DefaultTableModel();
        model=(DefaultTableModel) this.columnTable.getModel();

        for (int i=0;i<model.getRowCount();i++)
        {


            this.query=this.query + "`" + model.getValueAt(i, 0) + "` " + model.getValueAt(i,1);

            if (model.getValueAt(i, 2).toString().compareTo("true")==0)
            {
                this.query=this.query + " NOT NULL";
            }

            if (model.getValueAt(i, 4).toString().compareTo("")!=0)
            {
                this.query=this.query + " DEFAULT " + model.getValueAt(i, 4).toString();
            }

            if (model.getValueAt(i, 3).toString().compareTo("true")==0)
            {
                this.query=this.query + " AUTO_INCREMENT";
            }

            this.query = this.query + ",";

        }

        this.query = this.query + this.primarykey;

        if (this.query.charAt(this.query.length()-1)==',')
                this.query = this.query.substring(0,this.query.length()-1);
        this.query = this.query + ") ENGINE = " + this.engineComboBox.getModel().getSelectedItem().toString() + ";";
        this.queryText.setText(this.query);
        }
    }//GEN-LAST:event_columnTablePropertyChange

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
        // TODO add your handling code here:
        if (this.columnTable.getSelectedRow()==-1)
        {
            JOptionPane.showMessageDialog(this, "No row was selected for removal.","Warning",JOptionPane.WARNING_MESSAGE);
        }
        else
        {
            DefaultTableModel model1=new DefaultTableModel();
            model1=(DefaultTableModel) this.columnTable.getModel();
            Vector tabledata=new Vector();

            for (int i=0;i<model1.getRowCount();i++)
            {
                if (i!=this.columnTable.getSelectedRow())
                {
                    Vector v=new Vector();
                    for (int j=0;j<model1.getColumnCount();j++)
                    {
                        v.add(model1.getValueAt(i, j));
                    }
                    tabledata.add(v);
                }
            }

             Vector columnnames=new Vector();
             columnnames.add("Column Name");
             columnnames.add("Data Type");
             columnnames.add("Not NULL");
             columnnames.add("Auto INC");
             columnnames.add("Default Value");

             model1=new DefaultTableModel(tabledata,columnnames);
             this.columnTable.setModel(model1);

        }
    }//GEN-LAST:event_jButton9ActionPerformed

    private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed
        // TODO add your handling code here:
        if (this.columnTable.getSelectedRow()==-1)
        {
            JOptionPane.showMessageDialog(this, "No column was selected.","Warning",JOptionPane.WARNING_MESSAGE);
        }
        else
        {
        AddColumnDialog dialog=new AddColumnDialog(this,true);
        dialog.setTitle("Edit Column");
        dialog.setCoulumnNameField((String) this.columnTable.getModel().getValueAt(this.columnTable.getSelectedRow(), 0));
        dialog.setDataTypeBox((String) this.columnTable.getModel().getValueAt(this.columnTable.getSelectedRow(), 1));
        dialog.setNotNullCheckbox((Boolean) this.columnTable.getModel().getValueAt(this.columnTable.getSelectedRow(), 2));
        dialog.setAutoIncrementCheckbox((Boolean) this.columnTable.getModel().getValueAt(this.columnTable.getSelectedRow(), 3));
        dialog.setDefaultValueField((String) this.columnTable.getModel().getValueAt(this.columnTable.getSelectedRow(), 4));
        dialog.setVisible(true);

        if (dialog.getValues().size()>0)
        {
            Vector columnnames=new Vector();
            columnnames.add("Column Name");
            columnnames.add("Data Type");
            columnnames.add("Not NULL");
            columnnames.add("Auto INC");
            columnnames.add("Default Value");

            Vector tabledata=new Vector();
            DefaultTableModel model=new DefaultTableModel();
            model=(DefaultTableModel) this.columnTable.getModel();

            for (int i=0;i<model.getRowCount();i++)
            {

                Vector v1=new Vector();

                if (i==this.columnTable.getSelectedRow())
                {
                    v1=dialog.getValues();
                }
                else
                {
                    for (int j=0;j<model.getColumnCount();j++)
                    {
                        v1.add(model.getValueAt(i, j).toString());
                    }
                }

                tabledata.add(v1);
            }


            model=new DefaultTableModel(tabledata,columnnames);
            this.columnTable.setModel(model);
        }
        }
    }//GEN-LAST:event_jButton10ActionPerformed

    private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton12ActionPerformed
        // TODO add your handling code here:
        PrimaryKeyDialog dialog=new PrimaryKeyDialog(this,true);
        dialog.setColumns(this.columnTable.getModel());
        dialog.setVisible(true);
        if (dialog.getSelectedCoulumn().compareTo("")!=0)
        {
            this.primarykey=" PRIMARY KEY ('" + dialog.getSelectedCoulumn() + "'),";

             this.query="CREATE TABLE " + this.tableNameField.getText() + " (";

        DefaultTableModel model=new DefaultTableModel();
        model=(DefaultTableModel) this.columnTable.getModel();

        for (int i=0;i<model.getRowCount();i++)
        {


            this.query=this.query + "`" + model.getValueAt(i, 0) + "` " + model.getValueAt(i,1);

            if (model.getValueAt(i, 2).toString().compareTo("true")==0)
            {
                this.query=this.query + " NOT NULL";
            }

            if (model.getValueAt(i, 4).toString().compareTo("")!=0)
            {
                this.query=this.query + " DEFAULT " + model.getValueAt(i, 4).toString();
            }

            if (model.getValueAt(i, 3).toString().compareTo("true")==0)
            {
                this.query=this.query + " AUTO_INCREMENT";
            }

            this.query = this.query + ",";

        }

        this.query = this.query + this.primarykey;

        if (this.query.charAt(this.query.length()-1)==',')
                this.query = this.query.substring(0,this.query.length()-1);
        this.query = this.query + ") ENGINE = " + this.engineComboBox.getModel().getSelectedItem().toString() + ";";
        this.queryText.setText(this.query);
        }

    }//GEN-LAST:event_jButton12ActionPerformed

    private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton11ActionPerformed
        // TODO add your handling code here:
        if (this.tableNameField.getText().compareTo("")!=0)
        {
            ForeignKeyDialog dialog=new ForeignKeyDialog(this,true);
            dialog.setColumns(this.columnTable.getModel());
            dialog.setVisible(true);
        }
        else
        {
            JOptionPane.showMessageDialog(this, "Table name is empty","Warning",JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_jButton11ActionPerformed

    /**
    * @param args the command line arguments
    */
 

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem ExecuteMenuItem;
    private javax.swing.JRadioButton ascRadioButton;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JTable columnTable;
    private javax.swing.JTable deleteTable;
    private javax.swing.JRadioButton descRadioButton;
    private javax.swing.JRadioButton eRButton;
    private javax.swing.JComboBox engineComboBox;
    private javax.swing.JRadioButton gRButton;
    private javax.swing.JRadioButton geRButton;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton11;
    private javax.swing.JButton jButton12;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JMenuItem jMenuItem8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane10;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JScrollPane jScrollPane7;
    private javax.swing.JScrollPane jScrollPane8;
    private javax.swing.JScrollPane jScrollPane9;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JToggleButton jToggleButton2;
    private javax.swing.JRadioButton neRButton;
    private javax.swing.JTextArea queryText;
    private javax.swing.JRadioButton sRButton;
    private javax.swing.JRadioButton seRButton;
    private javax.swing.JList selectedList;
    private javax.swing.JList tableList;
    private javax.swing.JList tableList2;
    private javax.swing.JList tableList3;
    private javax.swing.JList tableList4;
    private javax.swing.JTextField tableNameField;
    private javax.swing.JList tablefieldList;
    private javax.swing.JTable updateTable;
    private javax.swing.JTextField whereField;
    // End of variables declaration//GEN-END:variables

}
